//
//  ViewController.m
//  AVDemo2
//
//  Created by Yan on 16/3/22.
//  Copyright © 2016年 baidu. All rights reserved.
//

#import "YHPViewController.h"
#import "Masonry.h"
#import "SoundPlayTools.h"
#import <AVFoundation/AVFoundation.h>


@interface YHPViewController ()

/***
 参数注释:
 * 1.startBtn 开始按钮
 * 2.endBtn 结束按钮
 * 3. @"buyao.wav"
 ***/
@property (nonatomic, strong) UIButton *startBtn;
@property (nonatomic, strong) UIButton *endBtn;
@property (nonatomic, strong) AVAudioRecorder * record;
@property (nonatomic, strong) AVAudioPlayer *player;
@end


@implementation YHPViewController

#pragma mark 懒加载属性
-(UIButton *)startBtn {
    if (!_startBtn) {
        _startBtn = [[UIButton alloc]init];
        _startBtn.backgroundColor = [UIColor yellowColor];
        [_startBtn setTitle:@"START" forState:UIControlStateNormal];
        [_startBtn setFont:[UIFont systemFontOfSize:28]];
        [_startBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_startBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    }
    return _startBtn;
}

-(UIButton *)endBtn {
    if (!_endBtn) {
        _endBtn = [[UIButton alloc]init];
        _endBtn.backgroundColor = [UIColor yellowColor];
        [_endBtn setTitle:@"END" forState:UIControlStateNormal];
        [_endBtn setFont:[UIFont systemFontOfSize:28]];
        [_endBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_endBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    }
    return _endBtn;
}

#pragma mark viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

#pragma mark 设置UI
-(void)setupUI {
    
    [self.view addSubview:self.startBtn];
    
    [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 50));
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view);
    }];
    [self.startBtn addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.endBtn];
    [self.endBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 50));
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(100);
    }];
    [self.endBtn addTarget:self action:@selector(end) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark button点击事件 
-(void)start {
//    [SoundPlayTools playSoundWithFileName:@"buyao.wav" isAlert:YES];
    [self record];
    NSLog(@"start");
}

-(void)end {
    [self.player pause];
}

// 内存警告
-(void)didReceiveMemoryWarning {
    [SoundPlayTools clearMemary];
}


#pragma mark - 录音
-(void)record {
    /***
     注释:
     * 1.创建录音地址
     * 2.录音对象创建
     * 3.准备录音
     * 4.录音
     }
#pragma mark 播放音乐
-(void)playAudio {
    /***
     注释:
     * 1.获取音乐资源路径
     * 2.创建播放音乐对象
     * 3.准备播放 播放
     ***/
    NSURL * url = [[NSBundle mainBundle] URLForResource:@"童话.mp3" withExtension:nil];
    NSError * error = nil;
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
    [self.player play];
    NSLog(@"play");
}


@end
